package com.example.hotelmanagement.rest;

import com.example.hotelmanagement.common.pojo.CommonResult;
import com.example.hotelmanagement.common.pojo.PageResult;
import com.example.hotelmanagement.domain.entity.User;
import com.example.hotelmanagement.domain.vo.UserPageReqVo;
import com.example.hotelmanagement.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author chenhetao
 * @version 1.0
 * @className UserController
 * @packageName com.example.hotelmanagement.rest
 * @date 2023/6/10 17:27
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @return 查询结果
     */
    @PostMapping("/page")
    public CommonResult<PageResult<User>> queryByPage(@RequestBody UserPageReqVo user) {
        return CommonResult.success(userService.queryByPage(user));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public CommonResult<User> queryById(@PathVariable("id") Long id) {
        return CommonResult.success(userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public CommonResult<User> add(@RequestBody User user) {
        return CommonResult.success(userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public CommonResult<User> update(@RequestBody User user) {
        return CommonResult.success(userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public CommonResult<Boolean> deleteById(@RequestParam("id")Long id) {
        return CommonResult.success(userService.deleteById(id));
    }

}
